//
//  MessageViewController.m
//  JinMaJiaDemo
//
//  Created by GripMeMory on 16/1/19.
//  Copyright © 2016年 GripMeMory. All rights reserved.
//

#import "MessageViewController.h"

#import "MessageCell.h"
#import "MessageModel.h"

#import "MessageDetailViewController.h"

#define kCellHeight 300 / 2
#define kShowViewLeftMargin 60 / 2

static NSString * cellIdentifier = @"messageCellName";


@interface MessageViewController () <UITableViewDelegate>

@property (nonatomic, strong) TableViewDataSource *dataSource;

@end

@implementation MessageViewController

{
    NSMutableArray *_messageList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self fakeValue];
    [self customNav];
    [self setupTableView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.tabBarController.tabBar.hidden = NO;
}

#pragma mark - Custom NavigationBar

- (void)fakeValue{
    _messageList = [NSMutableArray arrayWithCapacity:10];
    
    for (int i = 0; i < 10; i++) {
        MessageModel *model = [[MessageModel alloc] init];
        model.title = [NSString stringWithFormat:@"标题党标题%d", i];
        model.mainMessage = @"标题列表缩略，标题列表缩略标题列表缩略标题列表缩略标题列表缩略，标题列表缩略标题列表缩略，标题列表缩略标题列表缩略标题列表缩略标题列表缩略，标题列表缩略，标题列表缩略标题列表缩略标题列表缩略。";
        model.time = @"2015/11/27  08:13";
        [_messageList addObject:model];
    }
}

- (void)customNav{
    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonClick:)];
    self.navigationItem.leftBarButtonItem = bbi;
}

- (void)setupTableView{
    TableViewCellConfigurBlock configureBlock = ^(MessageCell *cell, MessageModel *model){
        [cell configureCellWithModel:model];
    };
    _dataSource = [[TableViewDataSource alloc] initWithItems:_messageList cellIdentifier:cellIdentifier configureBlock:configureBlock];
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UISCREENWIDTH, UISCREENHEIGHT) style:UITableViewStylePlain];
    [tableView registerClass:[MessageCell class] forCellReuseIdentifier:cellIdentifier];
    tableView.delegate = self;
    tableView.dataSource = _dataSource;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = RGB_COLOR(@"#c9c9c9");
    tableView.contentSize = CGSizeMake(UISCREENWIDTH, UISCREENHEIGHT - kNavigationBarHeight);
    [self.view addSubview:tableView];
}

#pragma mark - Actions

- (void)rightButtonClick:(UIBarButtonItem *)bbi{
    [self.navigationController popViewControllerAnimated:NO];
}

#pragma mark - TableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kCellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MessageDetailViewController *mdvc = [[MessageDetailViewController alloc] init];
    MessageModel *model = _messageList[indexPath.row];
    mdvc.title = model.title;
    [self.navigationController pushViewController:mdvc animated:NO];
}

@end
